﻿using System.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Z1029.NoteEditor
{
    public class EventDataEditorCtrl : MonoBehaviour
    {
        public EventBase selectEvent;
        public List<EventBase> selectEvents;
        public bool multiSelect;
        public Z1029WindowControl window;
        public RectTransform re;
        public Camera cam;
        public bool cloningNote;
        bool dataUpdating;

        //Model

        //@1:Single
        public EventBase.EventData data;

        //@2:Multi



        //View
        public InputField startTime;
        public InputField endTime;
        public InputField start;
        public InputField end;
        public InputField start2;
        public InputField end2;

        public void Init()
        {
            cam = LevelController.instance.viewCam;
            data = selectEvent.eventData;
            UpdateData();
        }

        void Update()
        {
            //快捷键
            if (Input.GetKeyDown(KeyCode.Delete))
            {
                Delete();
            }
            if (Input.GetKeyDown(KeyCode.Return))
            {
                Submit();
            }
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                Cancel();
            }

            //移动Event
            if (CanGenerateNote() && Input.GetMouseButton(1))
            {
                if (!multiSelect)
                {
                    float posY = new Vector3(cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - RectToMouse(re).x, Input.mousePosition.y - RectToMouse(re).y, LevelController.instance.camDistance)).x, cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - RectToMouse(re).x, Input.mousePosition.y - RectToMouse(re).y, 8)).y, 0).y;
                    int newT = Utils.GetBeatTimeFromTime(LevelController.instance.judgelines[0].DistanceToTime(posY, false), LevelController.instance.judgelines[0].bpm);
                    newT = Mathf.RoundToInt(newT / (LevelController.instance.nowDensity)) * (LevelController.instance.nowDensity);
                    
                    UpdateData();
                }
            }
            if (CanGenerateNote() && Input.GetMouseButtonDown(1))
            {
                if (multiSelect)
                {
                    float sy = new Vector3(cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - RectToMouse(re).x, Input.mousePosition.y - RectToMouse(re).y, LevelController.instance.camDistance)).x, cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - RectToMouse(re).x, Input.mousePosition.y - RectToMouse(re).y, 8)).y, 0).y;
                    //posX.text = sy.ToString() +"—" ;
                }
            }
            if (multiSelect && Input.GetMouseButtonUp(1))
            {
                float ey = new Vector3(cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - RectToMouse(re).x, Input.mousePosition.y - RectToMouse(re).y, LevelController.instance.camDistance)).x, cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - RectToMouse(re).x, Input.mousePosition.y - RectToMouse(re).y, 8)).y, 0).y;
                //posX.text = posX.text + ex.ToString();
            }
        }

        public void ChangeData()
        {
            if (!multiSelect)
            {
                throw new NotImplementedException();
            }
            else
            {
                throw new NotImplementedException();
            }
            UpdateData();
        }

        public void ChangeTypeValue()
        {
            if (!multiSelect)
            {
                throw new NotImplementedException();
            }
            else
            {
                throw new NotImplementedException();
            }
        }

        public void ChangeDirValue()
        {
            if (!multiSelect)
            {
                throw new NotImplementedException();
            }
            else
            {
                throw new NotImplementedException();
            }
        }

        public void AttachToBeatline()
        {
            if (!multiSelect)
            {
                throw new NotImplementedException();
            }
            else
            {
                throw new NotImplementedException();
            }
            CloseWindow();
        }

        public void UpdateData()
        {
            if (!multiSelect)
            {
                startTime.text = data.value.startTime.ToString();
                endTime.text = data.value.endTime.ToString();
                start.text = data.value.start.ToString();
                end.text = data.value.end.ToString();
                start2.text = data.value.start2.ToString();
                end2.text = data.value.end2.ToString();
                
            }
            else
            {
                throw new NotImplementedException();
            }
        }

        
        public bool CanGenerateNote()
        {
            return Input.mousePosition.x - RectToMouse(re).x < re.sizeDelta.x && Input.mousePosition.x - RectToMouse(re).x > 0;
        }

        public Vector2 RectToMouse(RectTransform rect)
        {
            Vector2 res = Vector2.zero;
            res.x = rect.anchoredPosition.x + Screen.width / 2 - rect.sizeDelta.x / 2;
            res.y = rect.anchoredPosition.y + Screen.height / 2 - rect.sizeDelta.y / 2;
            return res;
        }

        public void Submit()
        {
            if (!multiSelect)
            {
                throw new NotImplementedException();
            }
            else
            {
                throw new NotImplementedException();
            }
            CloseWindow();
        }

        public void CloseWindow()
        {
            EventDataEditManager.instance.CloseWindow();
        }

        public void Delete()
        {
            if (!multiSelect)
            {
                Destroy(selectEvent.gameObject);

            }
            else
            {
                for (int i = 0; i < selectEvents.Count; i++)
                {
                    Destroy(selectEvents[i].gameObject);
                }
            }
            NoteDataEditManager.instance.CloseWindow();
        }

        public void Cancel()
        {
            CloseWindow();
        }
    }
}